package com.zyx.javademo.juc.scheduled;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author Yaxi.Zhang
 * @since 2021/6/10 09:44
 * desc: ScheduledExecutorService快速入门

 *
 */
public class ScheExecServQuickStart {
    public static void main(String[] args) {
        ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
        executorService.scheduleAtFixedRate(()->{
            try {
                // 系统睡5s钟, 而 ScheduledExecutorService 中指定每隔 2s 执行一次
                // 观察 2s 后还是 5s 后执行输出语句
                Thread.sleep(5000);
            }catch (Exception ex){
                ex.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+" run : "+ System.currentTimeMillis());
            // 0延时，每隔2000毫秒执行一次
        }, 0, 2000, TimeUnit.MILLISECONDS);
    }
}
